package com.fyk.service.Impl;

import com.fyk.entity.Book;
import com.fyk.entity.User;
import com.fyk.mapper.BookMapper;
import com.fyk.mapper.UserBookMapper;
import com.fyk.service.BookService;
import com.fyk.utils.PageUtils;
import com.fyk.utils.ResultUtils;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author qazws
 * @Date 2022/8/20 16:02
 * @Version 1.0
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private UserBookMapper userBookMapper;

    @Override
    public ResultUtils selectAllBook(Book book) {
        PageHelper.startPage(book.getPage(),book.getNum());
        List<Book> books = bookMapper.selectBook(book);
        return new  ResultUtils(200,null, PageUtils.getPage(books));
    }

    @Override
    public ResultUtils insertBook(Book book) {
        bookMapper.insertBook(book);
        return new ResultUtils(200,"添加成功",null);
    }

    @Override
    public ResultUtils findBook(Book book) {
        System.out.println(book);
        PageHelper.startPage(book.getPage(),book.getNum());
        List<Book> books = bookMapper.selectBook(book);
        return new ResultUtils(200,null,PageUtils.getPage(books));
    }

    @Override
    public ResultUtils updateBook(Book book) {
        bookMapper.updateBook(book);
        return new ResultUtils(200,"修改成功！",null);
    }

    @Override
    public ResultUtils deleteBook(Book book) {
        System.out.println(11);
        bookMapper.deleteBook(book);
        return new ResultUtils(200,"删除成功！",null);
    }

    @Override
    public ResultUtils borrowBook(Book book) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        bookMapper.updateBook(book);
        userBookMapper.insertUserAndBook(user.getUserId(),book.getBookId());
        return new ResultUtils(200,"借阅成功",null);
    }

    @Override
    public ResultUtils returnBookList(Book book) {
        PageHelper.startPage(book.getPage(),book.getNum());
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer userId = user.getUserId();

        List<Book> books = bookMapper.selectBookByUserId(userId);
        return new ResultUtils(200,null,PageUtils.getPage(books));
    }

    @Override
    public ResultUtils returnFindBookList(Book book) {
        PageHelper.startPage(book.getPage(),book.getNum());
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        List<Book> books = bookMapper.selectBookByBookWriterOrBookName(book.getBookName(), book.getBookWriter(), userId);
        return new ResultUtils(200,null,PageUtils.getPage(books));
    }

    @Override
    public ResultUtils returnBook(Book book) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        Integer userId = user.getUserId();
        userBookMapper.deleteBook(book.getBookId(),userId);
        book = bookMapper.selectBookByBookId(book.getBookId());
        book.setBookNums(book.getBookNums() + 1);
        bookMapper.updateBook(book);
        return new ResultUtils(200,"还书成功",null);
    }

    @Override
    public ResultUtils delAllBook(Book book) {
        String[] strings = book.getBitId().split(",");
        List<String> list = Arrays.asList(strings);
        if (bookMapper.delAllBook(list) == 0){
            return new ResultUtils(500,"删除失败！",null);
        }
        return new ResultUtils(200,"删除成功！",null);
    }


}
